package cn.xiaoliu.product.controller;


import cn.xiaoliu.basic.exception.BusinessException;
import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.LoginContext;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.pet.domain.Pet;
import cn.xiaoliu.product.domain.Product;
import cn.xiaoliu.product.query.ProductQuery;
import cn.xiaoliu.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query){

        return productService.queryPage(query);
    }

    @PutMapping
    public AjaxResult submit(@RequestBody Product product, HttpServletRequest request){
        try {
            productService.submit(product, LoginContext.getLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    /**
     * 服务上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            productService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    /**
     * 服务下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            productService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    //删除服务
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
    /**
     * 查询宠物的基本信息和详情信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product queryById(@PathVariable("id") Long id){

        return productService.queryByIdDetail(id);
    }

    /**
     * 购买服务
     * @param id
     * @param request
     * @return
     */
   /* @GetMapping("/toBuy/{id}")
    public AjaxResult toBuy(@PathVariable("id") Long id, HttpServletRequest request){
        productService.toBuy(id,LoginContext.getLogininfo(request));
        return AjaxResult.me();
    }*/
}
